// miniprogram/pages/proxy_borrow/proxy_borrow.js
const db = require('../../utils/db');

let id;

Page({
  data: {
    book: {
      type: Object,
      value: {}
    },
    isDetailPage: {
      type: Boolean,
      value: false
    },
    busy: {
      type: Boolean,
      value: false
    },
    found: {
      type: Boolean,
      value: false
    },
    records: {
      type: Array,
      value: []
    },
    submitting: {
      type: Boolean,
      value: false
    }
  },

  onLoad: function (options) {
    id = options.id;
  },

  onReady: function () {
    this.setData({
      submitting: false
    });
    if (id) {
      this.setData({
        isDetailPage: true,
        busy: true,
        found: false
      });
      db.getBook(id).then(res => {
        this.setData({
          busy: false,
          found: res.data.length > 0,
          book: res.data[0] || {}
        });
      });
    } else {
      this.setData({
        isDetailPage: false
      });
    }
  },

  onShow: function () {

  },

  onHide: function () {

  },

  onUnload: function () {

  },

  onBorrowTap: function () {
    this.setData({
      submitting: true
    });
    db.makeBorrowDelivery(this.data.book._id).then(() => {
      this.setData({
        submitting: false
      });
      wx.showToast({
        title: '已预约借书',
      });
      wx.navigateBack();
    });
  }
})
